// pages/defect/adjustment/main/formula/index.js
import {
  _add,
  _list,
  _update,
  _remove
} from '../../../../api/workmanship-param-defect'
import {
  productList
} from '../../../../api/setting'
let modalInstance = null;
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    data: {
      type: Object,
      value: {},
      observer(data) {
        data?.['id'] && this.fetchList(data['id'])
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    dataSource: [],
    flawList: [],
    flawShow: false,
    modalShow: false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onAdd() {
      // const dataSource = [...this.data.dataSource];
      // dataSource.push({
      //   name: '',
      //   value: ''
      // })
      // this.setData({
      //   dataSource
      // })
      this.onModalShow();
    },
    onInput(event) {
      const {
        index,
        key
      } = event.currentTarget.dataset,
        dataSource = this.data.dataSource,
        value = String(dataSource[index][key]);
      wx.showModal({
        title: dataSource[index]['_name'],
        editable: true,
        content: value,
        placeholderText: '输入值',
        success: res => {
          if (res.confirm) {
            _update({
              finishProductSettingsId: dataSource[index]['finishProductSettingsId'],
              id: dataSource[index]['id'],
              numValue: res.content,
              workmanshipParamTypeId: dataSource[index]['workmanshipParamTypeId']
            }, () => {
              dataSource[index][key] = res.content;
              this.setData({
                dataSource
              })
            })
          }
        }
      })
    },
    onRemove(event) {
      const {
        index
      } = event.currentTarget.dataset,
        dataSource = this.data.dataSource;
      wx.showModal({
        title: '要移除此项吗？',
        success: res => {
          if (res.confirm) {
            _remove(dataSource[index], ()=>{
              dataSource.splice(index, 1);
              this.setData({
                dataSource
              })
            })
          }
        }
      })
    },
    onFlawCancel() {
      this.setData({
        flawShow: false
      })
    },
    onFlawShow(event) {
      const index = event.currentTarget.dataset?.index;
      if (index !== undefined) {
        this.updateParams(index);
      } else {
        modalInstance = event.detail;
      }
      const flawInstance = this.selectComponent("#flaw");
      flawInstance?.onSelectedData(this.data.dataSource.map(it=> it.finishProductSettingsId));
      this.setData({
        flawShow: true

      })
    },
    updateParams(idx) {
      const _this = this,
        dataSource = this.data.dataSource;
      modalInstance = {
        onOpenFlawCallback(data) {
          _update({
            finishProductSettingsId: data['id'],
            id: dataSource[idx]['id'],
            numValue: dataSource[idx]['numValue'],
            workmanshipParamTypeId: _this.properties.data['id']
          }, () => {
            dataSource[idx]['_name'] = data['name'];
            dataSource[idx]['finishProductSettingsId'] = data['id'];
            _this.setData({
              dataSource
            })
          })
        }
      }

    },
    onFlawSelect(event) {
      modalInstance?.onOpenFlawCallback?.(event.detail);
    },
    onModalCancel() {
      this.setData({
        modalShow: false
      })
    },
    onModalShow() {
      this.setData({
        modalShow: true
      })
    },
    fetchList(id) {
      _list({
        workmanshipParamTypeId: id || this.properties['data']['id']
      }, res => {

        this.data.flawList?.length > 0 ? (() => {
          const dataSource = res.map(it => {
            return {
              ...it,
              _name: this.data.flawList.find(item => {
                return item.id === it.finishProductSettingsId
              })?.['name']
            }
          });
          this.setData({
            dataSource
          })
        })() : this.fetchFlawList(res);
      })
    },
    fetchFlawList(dataSource) {
      productList({}, res => {
        const ds = dataSource.map(it => {
          return {
            ...it,
            _name: res.find(item => {
              return item.id === it.finishProductSettingsId
            })?.['name']
          }
        });
        this.setData({
          flawList: res,
          dataSource: ds
        })

      })
    },
    onModalSubmit(event) {
      const data = {
        numValue: event.detail['numValue'],
        finishProductSettingsId: event.detail['select']['id'],
        workmanshipParamTypeId: this.properties.data['id']
      }
      _add(data, () => {
        this.fetchList()
      })
    },
  }
})